package com.wg.dome.handle;

import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.GameConstant;
import com.wg.dome.attribute.Attribute;
import com.wg.dome.attribute.AttributeMgr;
import com.wg.dome.entity.user.HeroEntity;
import com.wg.dome.operation.ShangZhenRequest;
import com.wg.dome.operation.ShangZhenResponse;
import com.wg.dome.repository.HeroRepositoryEx;
import com.wg.core.net.OperationRequest;
import com.wg.core.net.OperationResponse;
import com.wg.core.net.SendParameters;

/**
 * 上阵英雄
 */
public class HandleRequest6 extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {
        ShangZhenRequest shangZhenRequest = new ShangZhenRequest(request);
        if (!shangZhenRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(shangZhenRequest);
        }

        if (shangZhenRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, shangZhenRequest.getPlayerId(), peer.getPlayerId());
        }

        HeroRepositoryEx heroRepository = new HeroRepositoryEx(peer.getPlayerId());

        HeroEntity heroEntity = heroRepository.getEntityFromPack(shangZhenRequest.getHeroId());
        if (heroEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.PACK_HEROID_NOT_EXIST, shangZhenRequest.getHeroId());
        }

        byte position = shangZhenRequest.getPosition();
        if (position > GameConstant.FIGHT_HERO_NUMBER) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.FIGHT_HEROIDX_OUT, position);
        }

        if (position == 0) {
            byte idlePos = heroRepository.getIdleSzPos();
            byte idelBzPos = heroRepository.getIdleBzPos();
            if (idlePos == 0) {
                return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.FIGHT_HERO_FULL);
            }
            heroEntity.setSzPos(idlePos);
            heroEntity.setBzPos(idelBzPos);
            heroRepository.update(heroEntity);
        } else {
            HeroEntity oldHeroEntty = heroRepository.getEntityBySzPos(position);
            if (oldHeroEntty == null) {
                return ErrorHelper.getErrorResponse(request.getOperationCode(), ErrorCode.FIGHT_HEROIDX_ERROR, position);
            }
            heroEntity.setSzPos(oldHeroEntty.getSzPos());
            heroEntity.setBzPos(oldHeroEntty.getBzPos());
            oldHeroEntty.setBzPos((byte) 0);
            oldHeroEntty.setSzPos((byte) 0);
            heroRepository.update(heroEntity);
            heroRepository.update(oldHeroEntty);
        }

        Attribute attribute = AttributeMgr.getFightAttribute(peer.getPlayerId(), heroEntity.getSzPos(), 2);

        ShangZhenResponse shangZhenResponse = new ShangZhenResponse();
        shangZhenResponse.setHeroId(heroEntity.getId());
        shangZhenResponse.setSzPos(heroEntity.getSzPos());
        shangZhenResponse.setSzAttribute(attribute.toHashTable());
        shangZhenResponse.setBzPos(heroEntity.getBzPos());
        return new OperationResponse(request.getOperationCode(), shangZhenResponse);
    }
}
